// pages/mineModule/memorandumInfo/memorandumInfo.js
import request from '../../../utils/request'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    mId:0,
    memorandumInfo:{
      id:1,
      title:"今日运动",
      content:"今天好开心，打了一天代码",
      dateTime:"2020/04/18"
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    console.log(options.id)
    // 如果不是add，表示为编辑以前的备忘录
    if(options.id != 'add'){
      request.get("/loadMemorandumInfoById", {id: options.id}).then(res => {
        this.setData({
          memorandumInfo: {
            id: res.data.id,
            title: res.data.title,
            content: res.data.content,
            dateTime: res.data.date
          }
        })
      })
      that.setData({
        mId: options.id
      })
      console.log(that.data.mId)
    }else{
      // 置空
      that.setData({
        memorandumInfo: {},
        mId: -1
      })
      
    }
  },
  userInputMemorandum: function (e) {
    let dataset = e.currentTarget.dataset;
    let value = e.detail.value;
    this.data.memorandumInfo[dataset.item] = value
    
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    var that = this
    if(that.data.mId==-1){
      /**增加进list */
      console.log(that.data.memorandumInfo.title);
      let data = {
        title: that.data.memorandumInfo.title,
        content: that.data.memorandumInfo.content
      }
      request.get("/insertMemorandumInfo", data).then(res => {
        console.log(res);
      })
    }else{
      /**修改list Item */
      let data = {
        id: that.data.mId,
        title: that.data.memorandumInfo.title,
        content: that.data.memorandumInfo.content
      }
      request.get("/updateMemorandumInfoById", data).then(res => {
        console.log(res);
      })
    }
  },
  saveMemorandumInfo: function () {

  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})